<?php
namespace app\admin\controller;
use think\Db;

class Banben extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $param = input();
        $param['page'] = intval($param['page']) <1 ? 1 : $param['page'];
        $param['limit'] = intval($param['limit']) <1 ? $this->_pagesize : $param['limit'];
        $where=[];

        $order='id desc';
        $res = model('Banben')->listData($where,$order,$param['page'],$param['limit']);


        $this->assign('list',$res['list']);
        $this->assign('total',$res['total']);
        $this->assign('page',$res['page']);
        $this->assign('limit',$res['limit']);

        $param['page'] = '{page}';
        $param['limit'] = '{limit}';
        $this->assign('param',$param);


        $this->assign('title','版本管理');
        return $this->fetch('admin@banben/index');
    }
    
    public function info()
    {
        if (Request()->isPost()) {
            $param = input('post.');
            if(isset($param['heimingdanshengshi']) && !empty($param['heimingdanshengshi'])){
                $param['heimingdanshengshi']=str_replace(['，',"\n"],[',',','],$param['heimingdanshengshi']);
                $param['heimingdanshengshi']=explode(',',$param['heimingdanshengshi']);
                foreach ($param['heimingdanshengshi'] as &$heimingdan){
                    $heimingdan = trim($heimingdan);
                }
                $param['heimingdanshengshi'] = array_filter($param['heimingdanshengshi']);
                $param['heimingdanshengshi'] = join(',',$param['heimingdanshengshi']);
            }
            if(isset($param['heimingdan_jumpshengshi']) && !empty($param['heimingdan_jumpshengshi'])){
                $param['heimingdan_jumpshengshi']=str_replace(['，',"\n"],[',',','],$param['heimingdan_jumpshengshi']);
                $param['heimingdan_jumpshengshi']=explode(',',$param['heimingdan_jumpshengshi']);
                foreach ($param['heimingdan_jumpshengshi'] as &$heimingdan){
                    $heimingdan = trim($heimingdan);
                }
                $param['heimingdan_jumpshengshi'] = array_filter($param['heimingdan_jumpshengshi']);
                $param['heimingdan_jumpshengshi'] = join(',',$param['heimingdan_jumpshengshi']);
            }
            $res = model('Banben')->saveData($param);
            if($res['code']>1){
                return $this->error($res['msg']);
            }
            return $this->success($res['msg']);
        }

        $id = input('id');
        $where=[];
        $where['id'] = ['eq',$id];
        $res = model('Banben')->infoData($where);
        
        $this->assign('info',$res['info']);
        $this->assign('title','版本管理');
        return $this->fetch('admin@banben/info');
    }

    public function del()
    {
        $param = input();
        $ids = $param['ids'];
        $all = $param['all'];

        if(!empty($ids)){
            $where=[];
            $where['id'] = ['in',$ids];
            if($all==1){
                $where['id'] = ['gt',0];
            }
            $res = model('Banben')->delData($where);
            if($res['code']>1){
                return $this->error($res['msg']);
            }
            return $this->success($res['msg']);
        }
        return $this->error('参数错误');
    }



}
